// 表单数据校验
function validateForm(name, age, email, interest) {
    if (!name || !age || !email || !interest) {
        alert('请填写完整信息');
        return false;
    }
    if (isNaN(age) || age < 0) {
        alert('年龄必须为有效数字');
        return false;
    }
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    if (!emailRegex.test(email)) {
        alert('请输入有效的邮箱地址');
        return false;
    }
    return true;
}

// 表单提交处理
function handleFormSubmit() {
    const name = document.getElementById('name').value;
    const age = document.getElementById('age').value;
    const email = document.getElementById('email').value;
    const interest = document.getElementById('interest').value;

    if (!validateForm(name, age, email, interest)) return;

    const newData = {
        id: Date.now(),
        name,
        age,
        email,
        interest,
        score1: 0,
        score2: 0,
        score3: 0,
        timestamp: new Date().toISOString().split('T')[0]
    };

    let csvData = localStorage.getItem('csvData');
    if (!csvData) {
        csvData = 'id,name,age,email,interest,score1,score2,score3,timestamp\n';
    }
    csvData += `${newData.id},${newData.name},${newData.age},${newData.email},${newData.interest},${newData.score1},${newData.score2},${newData.score3},${newData.timestamp}\n`;
    localStorage.setItem('csvData', csvData);

    renderTable();
    document.getElementById('dataForm').reset();
}

// 渲染表格
function renderTable() {
    const csvData = localStorage.getItem('csvData');
    if (!csvData) return;

    const rows = csvData.split('\n').filter(row => row);
    const headers = rows[0].split(',');
    const dataRows = rows.slice(1);

    const table = document.createElement('table');
    const thead = document.createElement('thead');
    const tbody = document.createElement('tbody');

    // 创建表头
    const headerRow = document.createElement('tr');
    headers.forEach(header => {
        const th = document.createElement('th');
        th.textContent = header;
        headerRow.appendChild(th);
    });
    thead.appendChild(headerRow);

    // 创建表格行
    dataRows.forEach(row => {
        const tr = document.createElement('tr');
        const cells = row.split(',');
        cells.forEach(cell => {
            const td = document.createElement('td');
            td.textContent = cell;
            tr.appendChild(td);
        });
        tbody.appendChild(tr);
    });

    table.appendChild(thead);
    table.appendChild(tbody);
    const tableContainer = document.getElementById('tableContainer');
    tableContainer.innerHTML = '';
    tableContainer.appendChild(table);
}

// CSV 文件上传处理
function handleFileUpload(event) {
    const file = event.target.files[0];
    if (!file) return;

    const reader = new FileReader();
    reader.onload = function(e) {
        localStorage.setItem('csvData', e.target.result);
        renderTable();
        renderCharts();
    };
    reader.readAsText(file);
}

window.addEventListener('load', () => {
    document.getElementById('dataForm').addEventListener('submit', (e) => {
        e.preventDefault();
        handleFormSubmit();
    });
    document.getElementById('fileInput').addEventListener('change', handleFileUpload);
    renderTable();
});
